using Clarity.Phone.Extensions;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Coding4Fun.Phone.Controls
{
	public abstract class PopUp<T, TPopUpResult> : Control
	{
		private DialogService dialogService_0;
		private PhoneApplicationPage phoneApplicationPage_0;
		private bool bool_0;
		private EventHandler<PopUpEventArgs<T, TPopUpResult>> eventHandler_0;
		private EventHandler eventHandler_1;
		public static readonly DependencyProperty OverlayProperty = DependencyProperty.Register("Overlay", typeof(Brush), typeof(PopUp<T, TPopUpResult>), new PropertyMetadata(Application.get_Current().get_Resources().get_Item("PhoneSemitransparentBrush")));
		[CompilerGenerated]
		private bool bool_1;
		[CompilerGenerated]
		private IApplicationBar iapplicationBar_0;
		[CompilerGenerated]
		private bool bool_2;
		[CompilerGenerated]
		private DialogService.AnimationTypes animationTypes_0;
		public event EventHandler<PopUpEventArgs<T, TPopUpResult>> Completed
		{
			add
			{
				EventHandler<PopUpEventArgs<T, TPopUpResult>> eventHandler = this.eventHandler_0;
				EventHandler<PopUpEventArgs<T, TPopUpResult>> eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler<PopUpEventArgs<T, TPopUpResult>> eventHandler3 = (EventHandler<PopUpEventArgs<T, TPopUpResult>>)Delegate.Combine(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler<PopUpEventArgs<T, TPopUpResult>>>(ref this.eventHandler_0, eventHandler3, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
			remove
			{
				EventHandler<PopUpEventArgs<T, TPopUpResult>> eventHandler = this.eventHandler_0;
				EventHandler<PopUpEventArgs<T, TPopUpResult>> eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler<PopUpEventArgs<T, TPopUpResult>> eventHandler3 = (EventHandler<PopUpEventArgs<T, TPopUpResult>>)Delegate.Remove(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler<PopUpEventArgs<T, TPopUpResult>>>(ref this.eventHandler_0, eventHandler3, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
		}
		public event EventHandler Opened
		{
			add
			{
				EventHandler eventHandler = this.eventHandler_1;
				EventHandler eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler eventHandler3 = (EventHandler)Delegate.Combine(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.eventHandler_1, eventHandler3, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
			remove
			{
				EventHandler eventHandler = this.eventHandler_1;
				EventHandler eventHandler2;
				do
				{
					eventHandler2 = eventHandler;
					EventHandler eventHandler3 = (EventHandler)Delegate.Remove(eventHandler2, value);
					eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.eventHandler_1, eventHandler3, eventHandler2);
				}
				while (eventHandler != eventHandler2);
			}
		}
		public bool IsOpen
		{
			get
			{
				return this.dialogService_0 != null && this.dialogService_0.IsOpen;
			}
		}
		public bool IsAppBarVisible
		{
			get;
			set;
		}
		internal IApplicationBar AppBar
		{
			get;
			set;
		}
		protected internal bool IsBackKeyOverride
		{
			get;
			set;
		}
		protected DialogService.AnimationTypes AnimationType
		{
			get;
			set;
		}
		public Brush Overlay
		{
			get
			{
				return (Brush)base.GetValue(PopUp<T, TPopUpResult>.OverlayProperty);
			}
			set
			{
				base.SetValue(PopUp<T, TPopUpResult>.OverlayProperty, value);
			}
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			if (this.dialogService_0 != null)
			{
				this.dialogService_0.SetAlignmentsOnOverlay(base.get_HorizontalAlignment(), base.get_VerticalAlignment());
			}
		}
		public virtual void OnCompleted(PopUpEventArgs<T, TPopUpResult> result)
		{
			this.bool_0 = true;
			if (this.eventHandler_0 != null)
			{
				this.eventHandler_0.Invoke(this, result);
			}
			if (this.dialogService_0 != null)
			{
				this.dialogService_0.Hide();
			}
			this.method_2();
		}
		public virtual void Show()
		{
			this.dialogService_0 = new DialogService
			{
				AnimationType = this.AnimationType,
				Child = this,
				BackgroundBrush = this.Overlay,
				IsBackKeyOverride = this.IsBackKeyOverride
			};
			if (!this.IsAppBarVisible)
			{
				this.AppBar = this.dialogService_0.Page.get_ApplicationBar();
				this.dialogService_0.Page.set_ApplicationBar(null);
			}
			this.dialogService_0.Closed += new EventHandler(this.method_1);
			this.dialogService_0.Opened += new EventHandler(this.method_0);
			this.phoneApplicationPage_0 = this.dialogService_0.Page;
			base.get_Dispatcher().BeginInvoke(new Action(this.method_3));
		}
		private void method_0(object sender, EventArgs e)
		{
			if (this.eventHandler_1 != null)
			{
				this.eventHandler_1.Invoke(sender, e);
			}
		}
		protected virtual TPopUpResult GetOnClosedValue()
		{
			return default(TPopUpResult);
		}
		public void Hide()
		{
			this.method_1(this, null);
		}
		private void method_1(object sender, EventArgs e)
		{
			if (!this.bool_0)
			{
				this.OnCompleted(new PopUpEventArgs<T, TPopUpResult>
				{
					PopUpResult = this.GetOnClosedValue()
				});
				return;
			}
			this.method_2();
		}
		private void method_2()
		{
			if (this.dialogService_0 != null)
			{
				if (!this.IsAppBarVisible && this.AppBar != null && this.phoneApplicationPage_0 != null)
				{
					this.phoneApplicationPage_0.set_ApplicationBar(this.AppBar);
				}
				this.phoneApplicationPage_0 = null;
				this.dialogService_0.Child = null;
				this.dialogService_0 = null;
			}
		}
		[CompilerGenerated]
		private void method_3()
		{
			this.dialogService_0.Show();
		}
	}
}
